﻿#region 命名空间

using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using Microsoft.Practices.EnterpriseLibrary.PolicyInjection;
using WinnerCMS.BLL;
using WinnerCMS.Common;
using WinnerCMS.Model;
using WinnerCMS.Model.Attribute;

#endregion

namespace WinnerCMS.WebUI.Admin.Member
{
    [Access("269002,269003,269004")]
    public partial class Manage : WebFactory.PageBase
    {
        private readonly MemberManage _myBll = PolicyInjection.Create<MemberManage>();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Check();

                BindList(1);
            }
        }

        private string Action
        {
            get
            {
                var obj = ViewState["Action"];
                if (obj == null)
                {
                    return null;
                }
                return obj.ToString();
            }
            set { ViewState["Action"] = value; }
        }

        private void Check()
        {
            this.ddlBatchToDo.Items[1].Enabled = RightsCheck.HasRight("269003");
            this.ddlBatchToDo.Items[2].Enabled = RightsCheck.HasRight("269001");
            this.ddlBatchToDo.Items[3].Enabled = RightsCheck.HasRight("269001");
        }

        protected void BindList(int pageNo)
        {
            this.GV_List.EmptyDataTemplate = GetTemplate(this);
            this.brPath.Items.Clear();

            var pp = new PagingParam {PageNo = pageNo, PageSize = this.PageNavigation1.PageSize};
            this.brPath.Items.Add(new ListItem("会员中心", "Member.aspx"));
            IList<User> list;
            if (Action == "Search")
            {
                this.brPath.Items.Add(new ListItem("会员搜索", ""));
                list = this._myBll.BindList(null, this.ddlSort.SelectedValue, this.txtKeyWord.Text, null, pp);
            }
            else if (Action == "GuoQi")
            {
                this.brPath.Items.Add(new ListItem(this.NtxtDays.Text + "天没有登录用户", ""));
                list = this._myBll.BindList(null, null, null, int.Parse(this.NtxtDays.Text), pp);
            }
            else if (Action == "YanZheng")
            {
                this.brPath.Items.Add(new ListItem("待验证用户", ""));
                list = this._myBll.BindList(null, "IsLocked", "2", null, pp);
            }
            else
            {
                list = this._myBll.BindList(null, null, null, null, pp);
            }
            this.GV_List.DataSource = list;
            this.GV_List.DataBind();
            this.PageNavigation1.RecordCount = pp.RecordCount;
        }


        protected string IDState(object idState)
        {
            switch (idState.ToString())
            {
                case "0":
                    return "激活";

                case "1":
                    return "封号";

                case "2":
                    return "待验证";

                default:
                    return "参数错误";
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            Action = "Search";
            BindList(1);
        }

        protected void btnGuoQi_Click(object sender, EventArgs e)
        {
            Action = "GuoQi";
            BindList(1);
        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            var res = this._myBll.BatchToDo(GetSelectedRow.GetIDs(this.GV_List), this.ddlBatchToDo.SelectedValue);
            if (string.IsNullOrEmpty(res.Errors) == false)
            {
                MsgBox.Message("操作成功！", this);
                BindList(1);
            }
            else
            {
                MsgBox.Message(res.Errors, this);
            }
        }

        protected void PageNavigation1_PageChanging(object src, WebControls.PageChangingEventArgs e)
        {
            BindList(e.NewPageIndex);
        }

        protected void lblYanZheng_Click(object sender, EventArgs e)
        {
            Action = "YanZheng";
            BindList(1);
        }
    }
}